package com.chushouya.manager.service.admin.impl;

import com.chushouya.common.support.Contexts;
import com.chushouya.manager.dto.admin.banner.*;
import com.general.framework.core.enums.StatusEnum;
import com.general.framework.core.lang.Dates;
import com.chushouya.manager.dao.entity.BannerEntity;
import com.chushouya.manager.dao.repository.BannerRepository;
//=====manager.dto.admin.banner.*;
import com.chushouya.manager.dto.admin.banner.*;
import com.chushouya.manager.service.admin.BannerService;
import com.general.framework.core.lang.Beans;
import com.general.framework.core.lang.Validate;
import com.general.framework.core.schema.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Banner横幅广告管理服务实现类
 * 提供后台管理相关的增删改查功能
 * 
 * @author chushouya
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerRepository bannerRepository;

    @Override
    public PageResult<BannerListDTO> selectPage(BannerQuery query) {
        PageResult<BannerEntity> page = bannerRepository.selectPage(query);
        return PageResult.of(page, BannerListDTO.class);
    }

    @Override
    public BannerDetailDTO detail(Long bannerId) {
        Validate.isNull(bannerId, "Banner ID不能为空");
        BannerEntity banner = bannerRepository.selectByPrimaryKey(bannerId);
        Validate.isNull(banner, "Banner不存在");
        return Beans.copy(banner, BannerDetailDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BannerSaveDTO saveDTO) {
        Validate.isNull(saveDTO, "保存数据不能为空");
        Validate.isBlank(saveDTO.getBannerName(), "Banner名称不能为空");
        Validate.isNull(saveDTO.getPositionId(), "位置ID不能为空");
        Validate.isBlank(saveDTO.getImage(), "图片地址不能为空");
        
        BannerEntity banner = Beans.copy(saveDTO, BannerEntity.class);
        banner.setCreateTime(Dates.getTimeNow());
        banner.setUpdateTime(Dates.getTimeNow());
        banner.setStatus(StatusEnum.normal.value()); // 默认启用
        banner.setHandleUser(Contexts.getUserContext().getUserName());
        
        bannerRepository.insertSelective(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BannerUpdateDTO updateDTO) {
        Validate.isNull(updateDTO, "更新数据不能为空");
        Validate.isNull(updateDTO.getBannerId(), "Banner ID不能为空");
        
        BannerEntity existBanner = bannerRepository.selectByPrimaryKey(updateDTO.getBannerId());
        Validate.isNull(existBanner, "Banner不存在");
        
        BannerEntity update = Beans.copy(updateDTO, BannerEntity.class);
        update.setUpdateTime(new Date());
        update.setHandleUser(Contexts.getUserContext().getUserName());
        
        bannerRepository.updateByPrimaryKeySelective(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long bannerId) {
        Validate.isNull(bannerId, "Banner ID不能为空");
        
        BannerEntity existBanner = bannerRepository.selectByPrimaryKey(bannerId);
        Validate.isNull(existBanner, "Banner不存在");
        
        // 软删除
        BannerEntity banner = new BannerEntity();
        banner.setBannerId(bannerId);
        banner.setDelFlag(1);
        banner.setUpdateTime(new Date());
        banner.setHandleUser(Contexts.getUserContext().getUserName());
        
        bannerRepository.updateByPrimaryKeySelective(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(List<Long> bannerIds) {
        Validate.isEmpty(bannerIds, "Banner ID列表不能为空");
        
        for (Long bannerId : bannerIds) {
            delete(bannerId);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Long bannerId, Integer status) {
        Validate.isNull(bannerId, "Banner ID不能为空");
        Validate.isNull(status, "状态不能为空");
        
        BannerEntity existBanner = bannerRepository.selectByPrimaryKey(bannerId);
        Validate.isNull(existBanner, "Banner不存在");
        
        BannerEntity banner = new BannerEntity();
        banner.setBannerId(bannerId);
        banner.setStatus(status);
        banner.setUpdateTime(new Date());
        
        bannerRepository.updateByPrimaryKeySelective(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSort(Long bannerId, Integer sort) {
        Validate.isNull(bannerId, "Banner ID不能为空");
        Validate.isNull(sort, "排序值不能为空");
        
        BannerEntity existBanner = bannerRepository.selectByPrimaryKey(bannerId);
        Validate.isNull(existBanner, "Banner不存在");
        
        BannerEntity banner = new BannerEntity();
        banner.setBannerId(bannerId);
        banner.setSort(sort);
        banner.setUpdateTime(new Date());
        
        bannerRepository.updateByPrimaryKeySelective(banner);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchUpdateSort(List<BannerSortDTO> sortList) {
        Validate.isEmpty(sortList, "排序列表不能为空");
        
        for (BannerSortDTO sortDTO : sortList) {
            updateSort(sortDTO.getBannerId(), sortDTO.getSort());
        }
    }
}
